package com.analysis.apresunday.service.impl;

import com.analysis.apresunday.config.ResultCode;
import com.analysis.apresunday.config.exception.GlobalException;
import com.analysis.apresunday.entity.po.User;
import com.analysis.apresunday.mapper.UserMapper;
import com.analysis.apresunday.entity.dto.UserLoginDTO;
import com.analysis.apresunday.entity.dto.UserRegisterDTO;
import com.analysis.apresunday.entity.dto.UserUpdateDTO;
import com.analysis.apresunday.entity.vo.UserInfoVo;
import com.analysis.apresunday.service.UserService;
import com.analysis.apresunday.utils.JwtUtils;
import com.analysis.apresunday.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author snake_skin
 * @since 2024-01-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void register(UserRegisterDTO userRegisterDto) {
        String userName = userRegisterDto.getUserName();
        String password = userRegisterDto.getPassword();
        String email = userRegisterDto.getEmail();
        String resourceTags = userRegisterDto.getResourceTags();
        String employmentTags = userRegisterDto.getEmploymentTags();

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)
                || StringUtils.isBlank(email) || StringUtils.isBlank(resourceTags)
                || StringUtils.isBlank(employmentTags)) {
            throw new GlobalException(ResultCode.ERROR,"请勿传空值！");
        }

        QueryWrapper<User> userNameQueryWrapper = new QueryWrapper<>();
        userNameQueryWrapper.eq("user_name", userName);
        User userNameUser = baseMapper.selectOne(userNameQueryWrapper);
        if (userNameUser != null) {
            throw new GlobalException(ResultCode.ERROR, "该用户名已存在");
        }

        QueryWrapper<User> emailQueryWrapper = new QueryWrapper<>();
        emailQueryWrapper.eq("email", email);
        User emailUser = baseMapper.selectOne(emailQueryWrapper);
        if (emailUser != null) {
            throw new GlobalException(ResultCode.ERROR, "该邮箱已存在");
        }

        User user = new User();
        user.setUserName(userName);
        user.setPassword(MD5.encrypt(password));
        user.setEmail(email);
        user.setResourceTags(resourceTags);
        user.setEmploymentTags(employmentTags);
        baseMapper.insert(user);

    }

    @Override
    public String login(UserLoginDTO userLoginDTO) {
        String userName = userLoginDTO.getUserName();
        String password = userLoginDTO.getPassword();
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            throw new GlobalException(ResultCode.ERROR,"用户名或密码不能为空");
        }

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name",userName);
        User user = baseMapper.selectOne(userQueryWrapper);
        if (user == null) {
            throw new GlobalException(ResultCode.ERROR,"该用户不存在！");
        }

        // 将密码进行md5加密后，与数据库中的结果进行对比
        if (!MD5.encrypt(password).equals(user.getPassword())) {
            throw new GlobalException(ResultCode.ERROR,"密码错误！");
        }

        // jwt加密后给前端传token
        return JwtUtils.getJwtToken(user.getId().toString(),user.getUserName());
    }

    @Override
    public UserInfoVo getLoginInfoByToken(HttpServletRequest request) {
        String userId = JwtUtils.getUserIdByJwtToken(request);
        User user = baseMapper.selectById(userId);
        if (user == null) {
            throw new GlobalException(ResultCode.ERROR,"token已过期或失效，请重新登录");
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setId(user.getId());
        userInfoVo.setUserName(user.getUserName());
        userInfoVo.setResourceTags(user.getResourceTags());
        userInfoVo.setEmploymentTags(user.getEmploymentTags());
        userInfoVo.setEmail(user.getEmail());
        return userInfoVo;
    }

    @Override
    public void updateUser(UserUpdateDTO userUpdateDto) {
        User user = baseMapper.selectById(userUpdateDto.getId());
        user.setUserName(userUpdateDto.getUserName());
        user.setEmail(userUpdateDto.getEmail());

        // 传过来的兴趣标签有可能是空的，是空的则不去改变
        if (!StringUtils.isBlank(userUpdateDto.getResourceTags())) {
            user.setResourceTags(userUpdateDto.getResourceTags());
        }

        if (!StringUtils.isBlank(userUpdateDto.getEmploymentTags())) {
            user.setEmploymentTags(userUpdateDto.getEmploymentTags());
        }

        baseMapper.updateById(user);

    }
}
